Imports Clo = Enhancements.Core.Logging
Imports Gen = System.Collections.Generic
Imports Glo = System.Globalization
Imports Iou = System.IO
Imports Ref = System.Reflection
Imports Reg = System.Text.RegularExpressions
Imports Sys = System
Imports Tex = System.Text
Imports Xsd = System.Xml.Schema
Imports W3c = System.Xml

Namespace Enhancements.Basic.Primitives
    ''' <summary>Represents a format applicable to a millisecond part in a System.DateTime instance.</summary>
    <Serializable(), DebuggerStepThrough()> _
    Public NotInheritable Class DateTimeFormatMillisecond
        Inherits DateTimeFormat

        ''' <summary>Initializes a DateTimeFormatMillisecond instance with default values.</summary>
        Public Sub New()
        End Sub

        ''' <summary>Initializes a DateTimeFormatMillisecond instance with defined values.</summary>
        ''' <param name="_digits">Number of digits used in the formatting result.</param>
        ''' <exception cref="IsInRangeNotException">_digits is lower than 1 or higher than 3.</exception>
        Public Sub New(ByVal _digits As Sys.Int32)
            Digits = _digits
        End Sub

        Private __Digits As Sys.Int32 = 3
        ''' <summary>Gets and sets the number of digits used in the formatting result. The default value is 3.</summary>
        ''' <exception cref="IsInRangeNotException">_value is lower than 1 or higher than 3.</exception>
        Public Property Digits() As System.Int32
            Get
                Return __Digits
            End Get
            Set(ByVal _value As System.Int32)
                _value.IsInRange("Digits._value", 1, 3, True)

                __Digits = _value
            End Set
        End Property

        ''' <summary>Builds a format expression according to the instance values.</summary>
        ''' <returns> </returns>
        Public Overrides Function ExpressionBuild() As System.String
            Dim _returnValueStringBuilder As Tex.StringBuilder = New Tex.StringBuilder(Sys.String.Empty)
            _returnValueStringBuilder.Append(___ExpressionPrefix)
            _returnValueStringBuilder.Append("millisecond")
            _returnValueStringBuilder.Append(___ExpressionSeparatorTitleProperties)
            _returnValueStringBuilder.Append("digits")
            _returnValueStringBuilder.Append(___ExpressionSeparatorPropertyNameValue)
            _returnValueStringBuilder.Append(Digits.ToString(Glo.CultureInfo.InvariantCulture))
            _returnValueStringBuilder.Append(___ExpressionSeparatorProperties)
            _returnValueStringBuilder.Append(___ExpressionSuffix)

            Return _returnValueStringBuilder.ToString()
        End Function

        ''' <summary>Formats a supplied value according to the instance values.</summary>
        ''' <param name="_value">Value to format.</param>
        ''' <returns> </returns>
        Public Overrides Function Format(ByVal _value As Sys.DateTime) As System.String
            Dim _returnValue As System.String = Sys.String.Empty
            _returnValue = _value.Millisecond.ToString(Glo.CultureInfo.InvariantCulture).PadLeft(3, "0"c).Substring(0, Digits)

            Return _returnValue
        End Function

        Friend Const ___RegularExpression As System.String = "^{millisecond:digits=(1|2|3);}$"

        ''' <summary>Tries to parse a format expression, returning an according instance in the _result parameter and True in the return value, when valid. When not, null is put in _result and False is returned.</summary>
        ''' <param name="_expression">Expression to inspect.</param>
        ''' <param name="_result">Instance returned when a valid format expression is used.</param>
        ''' <returns>Whether the operation proceeded normally.</returns>
        Public Shared Function TryParse(ByVal _expression As System.String, ByRef _result As DateTimeFormatMillisecond) As System.Boolean
            Dim _returnValue As System.Boolean = False
            Try
                _expression.IsInRegularExpression("_expression", ___RegularExpression, True)
                Dim _digits As System.Int32 = Sys.Int32.Parse(Reg.Regex.Matches(_expression, ___RegularExpression).Item(0).Groups(1).Value, Glo.CultureInfo.InvariantCulture)
                _result = New DateTimeFormatMillisecond(_digits)
                _returnValue = True
            Catch
                _result = Nothing
            End Try

            Return _returnValue
        End Function

    End Class
End Namespace
